package cn.iocoder.yudao.module.system.util.enums;

import cn.iocoder.yudao.framework.security.core.LoginUser;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.system.util.a_gaojian.DateTimeUtils;
import com.trs.hybase.client.TRSDatabaseColumn;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author zyk
 * 元数据默认字段管理
 */
public enum DefaultFieldEnum {
    /**
     * ID 主键
     */
    id("id", "bigint", "primary key", false, "", TRSDatabaseColumn.TYPE_CHAR),

    /**
     * 状态
     */
    status("status", "smallint", "not null", true, 0, TRSDatabaseColumn.TYPE_NUMBER),

    /**
     * 创建时间
     */
    createTime("create_time", "datetime", "", false, "", TRSDatabaseColumn.TYPE_DATE),

    /**
     * 创建人
     */
    creator("creator", "varchar2(200)", "", false, "", TRSDatabaseColumn.TYPE_PHRASE),

    /**
     * 修改时间
     */
    updateTime("update_time", "datetime", "", false, "", TRSDatabaseColumn.TYPE_DATE),

    /**
     * 修改人
     */
    updater("updater", "varchar2(200)", "", false, "", TRSDatabaseColumn.TYPE_PHRASE),


    /**
     * 是否删除
     */
    deleted("deleted", "smallint", "not null", true, "0", TRSDatabaseColumn.TYPE_NUMBER);

    /**
     * 字段名称
     */
    private String fieldName;

    /**
     * 字段类型
     */
    private String fieldType;

    /**
     * 约束
     */
    private String constraint;

    /**
     * 是否有默认值
     */
    private Boolean defaulted;

    /**
     * 默认值
     */
    private Object defaultValue;

    private Integer hybaseType;

    DefaultFieldEnum(String fieldName, String fieldType, String constraint, Boolean defaulted, Object defaultValue, Integer hybaseType) {
        this.fieldName = fieldName;
        this.fieldType = fieldType;
        this.constraint = constraint;
        this.defaulted = defaulted;
        this.defaultValue = defaultValue;
        this.hybaseType = hybaseType;
    }

    public static String createTableSQL() {
        StringBuffer sb = new StringBuffer();
        for (DefaultFieldEnum defaultFieldEnum : DefaultFieldEnum.values()) {
            sb.append(String.format("%s %s %s",
                    defaultFieldEnum.fieldName,
                    defaultFieldEnum.fieldType,
                    defaultFieldEnum.constraint
            ));
            if (defaultFieldEnum.defaulted) {
                sb.append(" default " + defaultFieldEnum.defaultValue);
            }
            sb.append(",");
        }
        return sb.toString();
    }

    public static String insertDataSql() {
        StringBuffer sb = new StringBuffer();
        for (DefaultFieldEnum defaultFieldEnum : DefaultFieldEnum.values()) {
            if (!defaultFieldEnum.fieldName.equals("id")) {
                sb.append(defaultFieldEnum.fieldName + ",");
            }
        }
        return sb.toString();
    }

    public static List<Object> defaultValue(Map<String, Object> metaData) {
        List<Object> params = new ArrayList<>();
        for (DefaultFieldEnum defaultFieldEnum : DefaultFieldEnum.values()) {
            if (!defaultFieldEnum.fieldName.equals("id")) {
                Object value = metaData.get(defaultFieldEnum.fieldName);
                if (value == null) {
                    params.add(defaultFieldEnum.getDefaultValue());
                } else {
                    if (StringUtils.isNotBlank(String.valueOf(value))) {
                        params.add(metaData.get(defaultFieldEnum.fieldName));
                    } else {
                        params.add(defaultFieldEnum.getDefaultValue());
                    }
                }
            }
        }
        return params;
    }

    public static StringBuffer insertDataQuestionSql(String seqName, Map<String, Object> map, List<Object> params) {
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        StringBuffer sb = new StringBuffer();
        for (DefaultFieldEnum defaultFieldEnum : DefaultFieldEnum.values()) {
            if (!defaultFieldEnum.fieldName.equals("id")) {
                if (map.containsKey(defaultFieldEnum.fieldName)) {
                    sb.append("?,");
                    Object value = map.get(defaultFieldEnum.fieldName);
                    if (value != null) {
                        params.add(value);
                    }else{
                        params.add(defaultFieldEnum.defaultValue);
                    }
                }else{
                    if(defaultFieldEnum.fieldType.equals("datetime")){
                        sb.append(String.format("'%s',", DateTimeUtils.getDate()));
                    }else if(defaultFieldEnum.fieldName.equals(DefaultFieldEnum.creator.fieldName)){
                        sb.append(String.format("%s,", loginUser.getId()));
                    }else if(defaultFieldEnum.fieldName.equals(DefaultFieldEnum.updater.fieldName)){
                        sb.append(String.format("%s,", loginUser.getId()));
                    }else{
                        sb.append(String.format("%s,", defaultFieldEnum.defaultValue));
                    }
                }
            }
        }
        sb.append("NEXTVAL('" + seqName + "')");
        return sb;
    }

    public String getFieldName() {
        return fieldName;
    }

    public String getFieldType() {
        return fieldType;
    }

    public Integer getHybaseType() {
        return hybaseType;
    }

    public String getConstraint() {
        return constraint;
    }


    public Boolean getDefaulted() {
        return defaulted;
    }

    public Object getDefaultValue() {
        return defaultValue;
    }

}
